﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace main
{
    public partial class main : baseClass.Forms.applicationForm
    {
        public main()
        {
            InitializeComponent();
            this.loginRequired = true;
            this.myRequirePermission = true;
            this.mainMenuStrip = this.myMainMenu;
        }

        private void InitSysMenu()
        {
            CommonTypes.Settings.sysMenuList.Clear();

            #region system
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.System, this.systemMenu);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.UserLogin, CommonTypes.FormCodes.System, this.loginMenu, false, false, false);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.ChangePass, CommonTypes.FormCodes.System, this.changePassMenuItem);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.UserList, CommonTypes.FormCodes.System, this.userMenuItem,true,true,true);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.Exit, CommonTypes.FormCodes.System, this.exitMenuItem, false, false, false);
            #endregion

            #region listing
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.Listing, this.listMenuStrip);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.WorkShift, CommonTypes.FormCodes.Listing, this.workShiftMenu);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.LocationList, CommonTypes.FormCodes.Listing, this.locationMenuItem);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.ProjectList, CommonTypes.FormCodes.Listing, this.projectMenuItem);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.ItemList, CommonTypes.FormCodes.Listing, this.productMenuItem);
            #endregion

            #region employee
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.Employment, this.employeeMenuStrip);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.EmployeeList, CommonTypes.FormCodes.Employment, this.emloyeeListMenu,true,true,true);
            #endregion

            #region wage
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.WageWorkTimeCount, CommonTypes.FormCodes.Wage, this.wgWorkTimeMenuItem, true, true, true);
            #endregion

            #region import
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.Import, this.imposrtStripMenuItem);
            CommonTypes.Settings.sysMenuList.Add(CommonTypes.FormCodes.ImportEmployee, CommonTypes.FormCodes.Import, this.importEmployeeMenuItem, true, true, true);
            #endregion
        }

        #region override
        public override void SetLanguage()
        {
            base.SetLanguage();
            this.InitSysMenu();
        }
        protected override bool CheckValid()
        {
            return true;
        }
        protected override bool WhenApplicationStart()
        {
            if (CommonTypes.Settings.sysDebugMode == false && Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                common.SysLibs.ShowMessage("Application is running.Only one instance of this application is allowed");
                return false;
            }

            bool retVal = true;
            baseClass.Forms.startSplash mySplash = new baseClass.Forms.startSplash();
            using (new common.PleaseWait(new Point(), mySplash))
            {
                DateTime time1 = DateTime.Now;
                try
                {
                    retVal = base.WhenApplicationStart();
                }
                catch (Exception er)
                {
                    retVal = false;
                    common.SysLibs.ShowError("systemError", er.Message);
                }
                if (retVal)
                {
                    //InitSystem(false);
                    //this.LogAccess = false;
                }
                while (true)
                {
                    if (mySplash == null || mySplash.IsDisposed) break;
                    DateTime time2 = DateTime.Now;
                    TimeSpan ts = time2 - time1;
                    if (ts.TotalMilliseconds > CommonTypes.Settings.sysSplashWaitInSecs * 1000) break;
                    common.SysLibs.WaitInMiliSecs(100);
                }
            }
            return retVal;
        }
        #endregion

        #region show form
        private void ShowWgBorrowForm(string statementId)
        {
            //wage.Forms.baseStatementPayment myForm = (wage.Forms.baseStatementPayment)this.myActiveForms.Find("wgBorrowForm");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new wage.Forms.baseStatementPayment();
            //    ((wage.Forms.baseStatementPayment)myForm).myDataType = application.Consts.constStatementType_wgPayment;
            //    ((wage.Forms.baseStatementPayment)myForm).SetTitle("TẠM ỨNG LƯƠNG", "Tam ung luong");
            //    myForm.myFormCode = wgBorowMenu.Tag.ToString();
            //    myForm.Name = "wgBorrowForm";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }
        private void ShowWgBorrowReturnForm(string statementId)
        {
            //forms.statementReceive myForm = (forms.statementReceive)this.myActiveForms.Find("wgBorrowReturnForm");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementReceive();
            //    ((forms.statementReceive)myForm).myDataType = application.Consts.constStatementType_wgReceive;
            //    ((wage.Forms.baseStatementPayment)myForm).SetTitle("HOÀN TẠM ỨNG", "Hoan tam ung");
            //    myForm.myFormCode = wgBorowReturnMenu.Tag.ToString();
            //    myForm.Name = "wgBorrowReturnForm";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }
        private void ShowWgLoanForm(string statementId)
        {
            //forms.statementLoan myForm = (forms.statementLoan)this.myActiveForms.Find("wgStatementLoan");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementLoan();
            //    ((forms.statementLoan)myForm).myDataType = application.Consts.constStatementType_wgLoan;
            //    myForm.myFormCode = wgLoanMenu.Tag.ToString();
            //    myForm.Name = "wgStatementLoan";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }

        private void ShowWgOutOfficeForm(string statementId)
        {
            //forms.statementOutOffice myForm = (forms.statementOutOffice)this.myActiveForms.Find("wgStatementOutOffice");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementOutOffice();
            //    ((forms.statementOutOffice)myForm).myDataType = application.Consts.constStatementType_wgOutOffice;
            //    myForm.myFormCode = wgOutOfficeMenu.Tag.ToString();
            //    myForm.Name = "wgStatementOutOffice";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }
        private void ShowWgProductForm(string statementId)
        {
            //forms.statementProduct myForm = (forms.statementProduct)this.myActiveForms.Find("statementProduct");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementProduct();
            //    ((forms.statementProduct)myForm).myDataType = application.Consts.constStatementType_wgProduct;
            //    myForm.myFormCode = wgProductMenu.Tag.ToString();
            //    myForm.Name = "statementProduct";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }

        private void ShowWgSalaryForm(string statementId)
        {
            //forms.statementSalary myForm = (forms.statementSalary)this.myActiveForms.Find("wgStatementSalary");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementSalary();
            //    ((forms.statementSalary)myForm).myDataType = application.Consts.constStatementType_wgEmSalay;
            //    myForm.myFormCode = wgSalaryChangeMenuItem.Tag.ToString();
            //    myForm.Name = "wgStatementSalary";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }

        private void ShowWgPolicyForm(string statementId)
        {
            //forms.statementPolicy myForm = (forms.statementPolicy)this.myActiveForms.Find("wgStatementPolicy");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementPolicy();
            //    ((forms.statementPolicy)myForm).myDataType = application.Consts.constStatementType_wgPolicy;
            //    myForm.myFormCode = wagePolicyMenu.Tag.ToString();
            //    myForm.Name = "wgStatementPolicy";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }
        private void ShowWgBonusForm(string statementId)
        {
            //forms.statementBonus myForm = (forms.statementBonus)this.myActiveForms.Find("wgStatementBonus");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementBonus();
            //    ((forms.statementBonus)myForm).myDataType = application.Consts.constStatementType_wgBonus;
            //    myForm.myFormCode = wagePolicyMenu.Tag.ToString();
            //    myForm.Name = "wgStatementBonus";
            //}
            //myForm.ShowData(statementId);
            //this.ShowForm(myForm, false);
        }

        #endregion


        private void main_Load(object sender, EventArgs e)
        {

        }
        private void licenseMenuItem_Click(object sender, EventArgs e)
        {
            //baseClass.interfaces.ShowLicenseCheck(application.sysLibs.sysProductOwner, application.sysLibs.sysProductCode, this.LicenseFileName());
        }

        #region system
        private void loginMenu_Click(object sender, EventArgs e)
        {
            try
            {
                if (ShowLogin() == false) QuitApplication();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }

        private void userMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string formCode = CommonTypes.FormCodes.UserList.ToString();
                baseClass.Forms.userList myForm = (baseClass.Forms.userList)this.myActiveForms.Find(formCode);
                if (myForm == null || myForm.IsDisposed)
                {
                    myForm = new baseClass.Forms.userList();
                    myForm.SetName(formCode);

                    this.myActiveForms.Add(myForm);
                }
                this.ShowForm(myForm, false);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }
        private void changePassMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string formCode = CommonTypes.FormCodes.ChangePass.ToString();
                baseClass.Forms.changePassword myForm = (baseClass.Forms.changePassword)this.myActiveForms.Find(formCode);
                if (myForm == null || myForm.IsDisposed)
                {
                    myForm = new baseClass.Forms.changePassword();
                    myForm.SetName(formCode);

                    this.myActiveForms.Add(myForm);
                }
                this.ShowForm(myForm, false);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }

        private void sysCodeEditMenu_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.SysCode.ToString();
            baseClass.Forms.sysCodeEdit myForm = (baseClass.Forms.sysCodeEdit)this.myActiveForms.Find(formCode);
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new baseClass.Forms.sysCodeEdit();
                myForm.SetName(formCode);

                this.myActiveForms.Add(myForm);
            }
            this.ShowForm(myForm, false);
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Kết thúc sử dụng chương trình", CommonTypes.Consts.constApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) this.Close();
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.About.ToString();
            baseClass.Forms.AboutBox myForm = new baseClass.Forms.AboutBox();
            this.ShowForm(myForm, true);
        }
        
        #endregion system

        #region list
        private void workShiftMenu_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.WorkShift.ToString();
            wage.Forms.workShiftList myForm = (wage.Forms.workShiftList)this.myActiveForms.Find(formCode);

            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new wage.Forms.workShiftList();
                myForm.SetName(formCode);
            }
            this.ShowForm(myForm, false);
        }

        private void projectMenu_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.ProjectList.ToString();
            baseClass.Forms.projectList myForm = (baseClass.Forms.projectList)this.myActiveForms.Find(formCode);
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new baseClass.Forms.projectList();
                myForm.SetName(formCode);
            }
            this.ShowForm(myForm, false);
        }

        private void productMenuItem_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.ItemList.ToString();
            baseClass.Forms.itemList myForm = (baseClass.Forms.itemList)this.myActiveForms.Find(formCode);
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new baseClass.Forms.itemList();
                myForm.SetName(formCode);
            }
            this.ShowForm(myForm, false);
        }

        private void locationMenuItem_Click(object sender, EventArgs e)
        {
            string formCode = CommonTypes.FormCodes.LocationList.ToString();
            baseClass.Forms.locationList myForm = (baseClass.Forms.locationList)this.myActiveForms.Find(formCode);
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new baseClass.Forms.locationList();
                myForm.SetName(formCode);
            }
            this.ShowForm(myForm, false);
        }

        private void statementProdStageMenuItem_Click(object sender, EventArgs e)
        {
            //Form myForm = this.myActiveForms.Find("statementProdStage");
            //if (myForm == null || myForm.IsDisposed)
            //{
            //    myForm = new forms.statementProdUnitCost();
            //    ((forms.statementProdUnitCost)myForm).myDataType = application.Consts.constStatementType_geProdUnitCost;
            //    myForm.Name = "statementProdStage";
            //}
            //this.ShowForm(myForm, false);
        }

        #endregion

        #region employee
        private void emloyeeListMenu_Click(object sender, EventArgs e)
        {
            Form myForm = this.myActiveForms.Find("employeeList");
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new employee.Forms.employeeList();
                myForm.Name = "employeeList";
            }
            this.ShowForm(myForm);
        }

        private void wgOutOfficeMenu_Click(object sender, EventArgs e)
        {
            ShowWgOutOfficeForm(null);
        }

        private void wgBorowMenu_Click(object sender, EventArgs e)
        {
            ShowWgBorrowForm(null);
        }
        private void wgBorowReturnMenu_Click(object sender, EventArgs e)
        {
            ShowWgBorrowReturnForm(null);
        }

        private void wgLoanMenu_Click(object sender, EventArgs e)
        {
            ShowWgLoanForm(null);
        }

        private void timeKeeperDataMenu_Click(object sender, EventArgs e)
        {
            Form myForm = this.myActiveForms.Find("TimeKeeperDataView");
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new wage.Forms.TimeKeeperDataView();
                myForm.Name = "TimeKeeperDataView";
            }
            this.ShowForm(myForm, false);
        }

        private void adjustTimeKeeperDataMenu_Click(object sender, EventArgs e)
        {
            Form myForm = this.myActiveForms.Find("TimeKeeperDataEdit");
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new wage.Forms.TimeKeeperDataEdit();
                myForm.Name = "TimeKeeperDataEdit";
            }
            this.ShowForm(myForm, false);
        }

        private void wageSheetMenu_Click(object sender, EventArgs e)
        {
            Form myForm = this.myActiveForms.Find("wageCount");
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new wage.Forms.wageCount();
                myForm.Name = "wageCount";
            }
            this.ShowForm(myForm, false);
        }

        private void wgSalaryChangeMenuItem_Click(object sender, EventArgs e)
        {
            ShowWgSalaryForm(null);
        }

        private void wagePolicyMenu_Click(object sender, EventArgs e)
        {
            ShowWgPolicyForm(null);
        }

        private void wgBonusMenuItem_Click(object sender, EventArgs e)
        {
            ShowWgBonusForm(null);
        }



        private void wgProductMenu_Click(object sender, EventArgs e)
        {
            ShowWgProductForm(null);
        }

        #endregion

        #region import
        private void importEmployeeMenuItem_Click(object sender, EventArgs e)
        {
            Form myForm = this.myActiveForms.Find("importEmployee");
            if (myForm == null || myForm.IsDisposed)
            {
                myForm = new import.Forms.importEmployee();
                myForm.Name = "importEmployee";
            }
            this.ShowForm(myForm);
        }
        #endregion

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void wgWorkTimeMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string formCode = CommonTypes.FormCodes.WageWorkTimeCount.ToString();
                wage.Forms.workTimeEntry myForm = (wage.Forms.workTimeEntry)this.myActiveForms.Find(formCode);
                if (myForm == null || myForm.IsDisposed)
                {
                    myForm = new wage.Forms.workTimeEntry();
                    myForm.SetName(formCode);
                    this.myActiveForms.Add(myForm);
                }
                this.ShowForm(myForm);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }
    }
}
